package mi.payment.constants;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import mi.payment.utils.iso8583.Utils;
import mi.payment.utils.secure.TerminalSecure;
import sunmi.sunmiui.utils.LogUtil;

/**
 * @author Lee64 on 2017/3/10.
 */
public class ComposeFieldUtils {

    /**
     * 只允许数字和=号
     */
    private static String stringFilter(String str) {
        String regEx = "[^0-9=]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    public static String compositionF036(String pan, String date, String cipher2, String cipher3) {
        String value = "";
        if (!TextUtils.isEmpty(pan)) {
            value += compositionEachTag("0A", pan);
        }
        if (!TextUtils.isEmpty(date)) {
            value += compositionEachTag("0E", date);
        }
        if (!TextUtils.isEmpty(cipher2)) {
            value += compositionEachTag("C2", cipher2.replaceAll("=", "D"));
            LogUtil.e("nsz", "track2:" + value);
        }
        if (!TextUtils.isEmpty(cipher3)) {
            String fil = stringFilter(cipher3);
            fil = fil.replaceAll("=", "D");
            int indexOf = fil.indexOf(pan);
            if (indexOf != -1) {
                String c3 = fil.substring(indexOf, fil.length());
                String sub = fil.substring(0, indexOf);
                LogUtil.e("nsz", c3 + "---" + sub);
                if (!TextUtils.equals("99", sub)) {
                    sub = "99";
                }
                fil = sub + c3;
            }
            LogUtil.e("nsz", "track3:" + fil);
            value += compositionEachTag("C3", fil);
        }

        int i = value.length() % 16;
        if (i != 0) {
            for (int j = 0; j < 16 - i; j++) {
                value += "0";
            }
        }
        LogUtil.e("nsz", "36域加密前数据:" + value);
        byte[] bytes = Utils.hexStr2Bytes(value);

        LogUtil.e("nsz", "36域加密前数据转化:" + Utils.byte2HexStr(bytes));

        String res = "";
        for (int j = 0; j < bytes.length / 8; j++) {
            byte[] srcByte = new byte[8];
            byte[] resByte = new byte[8];
            System.arraycopy(bytes, j * 8, srcByte, 0, 8);
            int result = TerminalSecure.dataEncrypt(TerminalSecure.TDKIndex, 5, 3, srcByte, resByte);
            LogUtil.e("nsz", "j：" + j + " result：" + result);
            res += Utils.byte2HexStr(resByte);
        }

        LogUtil.e("nsz", "36域加密后数据:" + res);

        return res;
    }

    private static String compositionEachTag(String tag, String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        String value;
        String len = Integer.toHexString(str.length());
        if (len.length() == 1) {
            len = "0" + len;
        }
        if (str.length() % 2 == 0) {
            value = tag + len + str;
        } else {
            value = tag + len + str + "0";
        }
        return value;
    }

}
